package com.voxel.sense.infomsg.module.common.dropmenu;

import android.support.annotation.LayoutRes;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.interfaces.listener.OnDropMenuButtonListener;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.view.dropmenu.DropDownMenu;
import com.voxel.sense.infomsg.widget.adapter.FilterRecyclerSpareAdapter;
import com.voxel.sense.infomsg.widget.adapter.FilterSuperviseRecyclerAdapter;
import com.voxel.sense.infomsg.widget.adapter.AbsPickRecyclerAdapter;
import com.voxel.sense.infomsg.widget.adapter.TextFilterRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;

/**
 * 管理公司列表菜单筛选栏的工具类
 * 菜单栏的item排序是根据调用buid方法的先后进行排序
 * Created by ChenYiYao on 2018/8/11.
 */
public class DropPickMenuBuilder implements DropPickMenuManager<List<ConditionBean>>,
        IMenuBuilder<ConditionBean> {

    private View mContextView; //主内容的view
    private DropDownMenu drop_down_menu; //菜单控件
    private View industry_recycler;//行业列表
    private View supervise_view;//监管列表

    private FilterRecyclerSpareAdapter mIdustryAdapter; //行业类型适配器
    private AbsPickRecyclerAdapter mSmartSortAdapter; //违法记录类型适配器 inherite
    private FilterSuperviseRecyclerAdapter mSuperviseNatureAdapter; //监管性质及等级适配器


    private OnDropMenuButtonListener mResetAndSubimitListener; //重置监听
    private RecyclerAdapter.AdapterListenerImpl onItemClickListener;

    private List<String> headers = new ArrayList<>();
    private List<View> popupViews = new ArrayList<>();
    private FilterSuperviseRecyclerAdapter mSuperviseLevelAdapter;
    private View mDepartRoot;
    private FilterRecyclerSpareAdapter mDepartAdapter; //部门
    private View mRoleType;
    private FilterRecyclerSpareAdapter mRoleTypeAdapter;

    /**
     * @param drop_down_menu 菜单控件
     * @param view           主内容列表
     */
    public DropPickMenuBuilder(DropDownMenu drop_down_menu, View view) {
        this.drop_down_menu = drop_down_menu;
        mContextView = view;
    }

    @Override
    public DropPickMenuManager build() {
        drop_down_menu.setDropDownMenu(headers, popupViews, mContextView);
        return this;
    }

    @Override
    public IMenuBuilder buildIndustryList() {
        initIdustryList();
        return this;
    }

    @Override
    public IMenuBuilder buildSmartList() {
        createSmartList();
        return this;
    }

    @Override
    public IMenuBuilder buildDepartmentList() {
        initDepartmentList();
        return this;
    }


    @Override
    public IMenuBuilder builRoleType() {
        initRoleTypeList();
        return this;
    }

    private void initRoleTypeList() {

        if (mRoleType != null)
            return;

        //初始化view
        mRoleType = initTipView(Constants.TYPE_USER_ROLE, R.layout.item_drop_menu);

        RecyclerView recycler = mRoleType.findViewById(R.id.recycle_menu);

        initRecycler(recycler);
        //初始化适配器
        mRoleTypeAdapter = new FilterRecyclerSpareAdapter(onItemClickListener, Constants.TYPE_USER_ROLE);

        recycler.setAdapter(mRoleTypeAdapter);

        //将view添加到窗口中
        headers.add(Factory.app().getString(R.string.drop_menu_role_type_text));
        popupViews.add(mRoleType);

    }

    private void initDepartmentList() {
        //判断是否已存在，以免重复添加
        if (mDepartRoot != null)
            return;

        //初始化view
        mDepartRoot = initTipView(Constants.TYPE_DEPARTMENT_TYPE, R.layout.item_drop_menu);

        RecyclerView recycler = mDepartRoot.findViewById(R.id.recycle_menu);

        initRecycler(recycler);
        //初始化适配器
        mDepartAdapter = new FilterRecyclerSpareAdapter(onItemClickListener, Constants.TYPE_DEPARTMENT_TYPE);

        recycler.setAdapter(mDepartAdapter);

        //将view添加到窗口中
        headers.add(Factory.app().getString(R.string.drop_menu_user_typet_text));
        popupViews.add(mDepartRoot);
    }

    @Override
    public IMenuBuilder buildSuperviseList() {
        initSuperviseList();
        return this;
    }

    /**
     * 创建行业类型列表
     */
    private void initIdustryList() {
        //判断是否已存在，以免重复添加
        if (industry_recycler != null)
            return;

        //初始化view
        industry_recycler = initTipView(Constants.TYPE_IDUSTRY, R.layout.item_drop_menu);

        RecyclerView recycler = industry_recycler.findViewById(R.id.recycle_menu);
        recycler.setBackgroundResource(R.color.white);
        //创建流式布局管理器
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(Factory.app());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        //初始化适配器
        mIdustryAdapter = new FilterRecyclerSpareAdapter(onItemClickListener, Constants.TYPE_IDUSTRY);

        recycler.setLayoutManager(layoutManager);
        recycler.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp_white));
        recycler.setAdapter(mIdustryAdapter);

        //将view添加到窗口中
        headers.add(Factory.app().getString(R.string.drop_menu_industry_text));
        popupViews.add(industry_recycler);
    }


    /**
     * 设置行业类型数据
     *
     * @param data
     */
    public void setIdustryData(List data) {
        if (mIdustryAdapter != null) {
            mIdustryAdapter.replace(data);
        }
    }

    /**
     * 初始化监管类型筛选列表
     */
    private void initSuperviseList() {
        if (supervise_view != null) {
            //避免重复创建
            return;
        }

        supervise_view = initTipView(Constants.TYPE_SUPERVISE, R.layout.item_drop_menu_supervice);
        RecyclerView natureRecycler = supervise_view.findViewById(R.id.recycle_nature);
        RecyclerView levelRecycler = supervise_view.findViewById(R.id.recycle_level);
        if (natureRecycler != null) {
            //初始化监管性质列表
            natureRecycler.setLayoutManager(new GridLayoutManager(Factory.app(), 3));
            //设置下划线
            natureRecycler.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp_white));
            //创建适配器
            mSuperviseNatureAdapter = new FilterSuperviseRecyclerAdapter(onItemClickListener);
            mSuperviseNatureAdapter.setSingleSelect(true);
            natureRecycler.setAdapter(mSuperviseNatureAdapter);
        }

        if (levelRecycler != null) {
            //初始化监管等级列表
            levelRecycler.setLayoutManager(new LinearLayoutManager(Factory.app()));
            //设置下划线
            levelRecycler.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp_white));
            //创建适配器
            mSuperviseLevelAdapter = new FilterSuperviseRecyclerAdapter(onItemClickListener);
            mSuperviseLevelAdapter.setSingleSelect(true);
            levelRecycler.setAdapter(mSuperviseLevelAdapter);
        }

        //添加到菜单窗口
        headers.add(Factory.app().getString(R.string.drop_menu_supervise_text));
        popupViews.add(supervise_view);

    }

    /**
     * 创建智能筛选类型列表
     */
    private void createSmartList() {
        //初始化view
        View root = initTipView(Constants.TYPE_SMART_SORT, R.layout.item_drop_menu);
        RecyclerView recycler = root.findViewById(R.id.recycle_menu);

        //初始化监管等级列表
        recycler.setLayoutManager(new LinearLayoutManager(Factory.app()));
        //设置下划线
        recycler.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider));
        //创建适配器
        initRecycler(recycler);
        //初始化适配器
        mSmartSortAdapter = new TextFilterRecyclerAdapter(onItemClickListener);
        mSmartSortAdapter.setSingleSelect(true);
        recycler.setAdapter(mSmartSortAdapter);
        //将view添加到窗口中
        headers.add(Factory.app().getString(R.string.drop_menu_smart_text));
        popupViews.add(root);
    }


    /**
     * 初始化某个列表
     */
    private View initTipView(String type, @LayoutRes int resId) {
        //地址选中列表
        View root = LayoutInflater.from(Factory.app()).inflate(resId, null);
        View resetView = root.findViewById(R.id.btn_reset);
        View submitView = root.findViewById(R.id.btn_submit);

        //添加事件
        if (submitView != null) {
            initMenuSubmitListener(type, submitView);
        }
        if (resetView != null) {
            initMenuResetEvent(type, resetView);
        }
        return root;
    }

    private void initRecycler(RecyclerView recy) {
        recy.setBackgroundResource(R.color.white);
        //创建流式布局管理器
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(Factory.app());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setAlignItems(AlignItems.STRETCH);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        //初始化适配器
        recy.setLayoutManager(layoutManager);
        recy.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp_white));
    }

    @Override
    public void setSuperviseNatureData(List<ConditionBean> data) {
        if (mSuperviseNatureAdapter != null) {
            mSuperviseNatureAdapter.replace(data);
        }
    }

    @Override
    public void setSuperviseLevelData(List<ConditionBean> data) {
        if (mSuperviseLevelAdapter != null) {
            mSuperviseLevelAdapter.replace(data);
        }
    }

    @Override
    public void setFirmSmartSortData(List<ConditionBean> data) {
        if (mSmartSortAdapter != null) {
            mSmartSortAdapter.replace(data);
        }
    }

    @Override
    public void setDeparmentTypeData(List<ConditionBean> data) {
        if (mDepartAdapter != null) {
            mDepartAdapter.replace(data);
        }
    }

    @Override
    public void setRoleTypeData(List<ConditionBean> data) {
        if (mRoleTypeAdapter != null) {
            mRoleTypeAdapter.replace(data);
        }

    }


    /**
     * 重置菜单数据
     *
     * @param type_idustry 类型
     * @param resetView    监听的view
     */
    private void initMenuResetEvent(final String type_idustry, View resetView) {
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mResetAndSubimitListener != null) {
                    mResetAndSubimitListener.onResetClick(type_idustry);
                }
                switch (type_idustry) {
                    case Constants.TYPE_IDUSTRY://行业类型
                        mIdustryAdapter.reSetStatus();
                        break;
                    case Constants.TYPE_SUPERVISE://监管
                        mSuperviseLevelAdapter.reSetStatus();
                        mSuperviseNatureAdapter.reSetStatus();
                        break;
                    case Constants.TYPE_SMART_SORT://公司列表智能筛选
                        mSmartSortAdapter.reSetStatus();
                        break;
                    case Constants.TYPE_DEPARTMENT_TYPE://部门类型
                        mDepartAdapter.reSetStatus();
                        break;
                    case Constants.TYPE_USER_ROLE://角色
                        mRoleTypeAdapter.reSetStatus();
                        break;
                }
            }
        });
    }

    /**
     * 提交按钮
     *
     * @param type 类型
     * @param view 监听的view
     */
    private void initMenuSubmitListener(final String type, View view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mResetAndSubimitListener != null) {
                    mResetAndSubimitListener.onSubmitClick(type);
                }
            }
        });
    }

    /**
     * 添加单个条件item点击事件
     */
    @Override
    public IMenuBuilder addItemOnclickListener(RecyclerAdapter.AdapterListenerImpl listener) {
        onItemClickListener = listener;
        if (mIdustryAdapter != null) {
            mIdustryAdapter.setListener(onItemClickListener);
        }
        if (mSuperviseLevelAdapter != null) {
            mSuperviseLevelAdapter.setListener(onItemClickListener);
        }
        if (mSuperviseNatureAdapter != null) {
            mSuperviseNatureAdapter.setListener(onItemClickListener);
        }
        if (mSmartSortAdapter != null) {
            mSmartSortAdapter.setListener(onItemClickListener);
        }
        if (mDepartAdapter != null) {
            mDepartAdapter.setListener(onItemClickListener);
        }
        if (mRoleTypeAdapter != null) {
            mRoleTypeAdapter.setListener(onItemClickListener);
        }
        return this;
    }

    @Override
    public IMenuBuilder addResetAndSubmitListener(OnDropMenuButtonListener resetListener) {
        mResetAndSubimitListener = resetListener;
        return this;
    }


}
